using UnityEngine;

namespace GameEntry
{
    /// <summary>
    /// 游戏程序集清单配置
    /// </summary>
    [CreateAssetMenu(menuName = "@HoweGameFramework/" + nameof(GameAssemblyManifest),
        fileName = nameof(GameAssemblyManifest))]
    public class GameAssemblyManifest : ScriptableObject
    {
        /// <summary>
        /// 版本号
        /// </summary>
        public string version = "v1.0.0";

        /// <summary>
        /// 游戏逻辑程序集
        /// </summary>
        public string[] gameLogicAssemblyNames;

        /// <summary>
        /// 游戏逻辑程序集资源键值字符串格式化参数
        /// </summary>
        public string gameLogicAssemblyAssetKeyFormat = "{0}";

        /// <summary>
        /// AOT补充元数据程序集
        /// </summary>
        public string[] aotPatchAssemblyNames;

        /// <summary>
        /// AOT补充元数据程序集资源键值字符串格式化参数
        /// </summary>
        public string aotPatchAssemblyAssetKeyFormat = "{0}";

        /// <summary>
        /// 游戏逻辑程序集入口类名
        /// </summary>
        public string gameLogicEntryTypeName = "GameLogic.GameLogicEntry";

        /// <summary>
        /// 游戏逻辑程序集入口方法名
        /// </summary>
        public string gameLogicEntryStaticMethodName = "Init";

        /// <summary>
        /// 获取游戏逻辑程序集资源键值
        /// </summary>
        public string GetGameLogicAssemblyAssetKey(string assemblyName)
        {
            return string.Format(gameLogicAssemblyAssetKeyFormat, assemblyName);
        }

        /// <summary>
        /// 获取AOT补充元数据程序集资源键值
        /// </summary>
        public string GetAOTPatchAssemblyAssetKey(string assemblyName)
        {
            return string.Format(aotPatchAssemblyAssetKeyFormat, assemblyName);
        }

#if UNITY_EDITOR
        public static GameAssemblyManifest GetOrCreateManifest()
        {
            const string path = "Assets/HybridCLRData/GameAssemblyManifest.asset";
            var manifest = UnityEditor.AssetDatabase.LoadAssetAtPath<GameAssemblyManifest>(path);
            if (manifest != null)
                return manifest;

            manifest = ScriptableObject.CreateInstance<GameAssemblyManifest>();
        
            UnityEditor.AssetDatabase.CreateAsset(manifest, path);
            UnityEditor.AssetDatabase.SaveAssets();

            return manifest;
        }
#endif
    }
}
